package com.unsiv.blplayer.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.media.MediaPlayer;
import android.os.SystemClock;

/**
 * 
 * @author fanzhen
 * @time   9:53:30 PM Oct 6, 2011
 * @email  fanzhen.meng@gmail.com
 */
public final class MyMediaHelper{
	public static MediaPlayer mp = new MediaPlayer();
	/**
	 * 播放字节流
	 * @param mp3SoundByteArray
	 */
	public static void playFromByteArray(byte[] mp3SoundByteArray) {
		try {
			File tempMp3 = File.createTempFile(String.valueOf(SystemClock.uptimeMillis()), "mp3",new File(MyFileHelper.G_TempPath));
			tempMp3.deleteOnExit();
			FileOutputStream fos = new FileOutputStream(tempMp3);
			fos.write(mp3SoundByteArray);
			fos.close();
			FileInputStream fis = new FileInputStream(tempMp3);
			mp.setDataSource(fis.getFD());
			mp.prepare();
			mp.start();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
	/**
	 * 播放文件
	 * @param absolutePath
	 */
	public static void playFromAbsolutePath(String absolutePath) {
		try {
			mp.reset();
			mp.setDataSource(absolutePath);
			mp.prepare();
			mp.start();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}